<?php
	class Session
	{
		public static $started=false;
		public static $id=0;
		
		
		/**
		 * @desc Return the session id. If the session
		 * is not started yet, it starts the session.
		 * @return int (session_id)
		 */
		public static function id(){
			if (self::$started){
				return self::$id;
			}else{
				self::start();
				return self::$id;
			}
		}
		
		
		/**
		 * @desc Start the session. Assign session
		 * variable to local variable.
		 */
		public static function start(){
			if (!self::$started){
				session_start();
				self::$started=true;
				self::$id=session_id();
			}
		}
		
		
		/**
		 * @desc Reset the session. This
		 * is useful for login/logout.
		 */
		public static function reset(){
			if (!self::$started) self::start();
			session_regenerate_id();
			self::$id=session_id();
		}
		
		
		/**
		 * @desc Destroy the current session.
		 */
		public static function destroy(){
			session_destroy();
			self::$started=false;
		}
		
		
		/**
		 * @desc Register a variable to the session.
		 * @param string $name The name of the variable
		 * @param string $value The value of the variable
		 */
		public static function set($name,$value){
			confirm(self::$started,'invalid');
			if (is_object($value)){
				exit("invalid");
			}
			$_SESSION[$name]=$value;
		}
		
		
		/**
		 * @desc Return the value of the session variable.
		 * @param string $name The name of the variable.
		 */
		public static function get($name){
			confirm(self::$started,'invalid');
			confirm(isset($_SESSION[$name]),"invalid $name");
			return $_SESSION[$name];
		}
		
		
		/**
		 * @desc Check if some session variable is already existed.
		 * @param string $name
		 * @return true/false
		 */
		public static function has($name){
			if (isset($_SESSION[$name])){
				return true;
			}
			return false;
		}
	}
?>